package nc.impl.gsp.pub.bill;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import nc.bs.dao.BaseDAO;
import nc.itf.gsp.pub.bill.IGspBillQuery;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.pub.SuperVO;


public class GspBillQueryImpl implements IGspBillQuery {

	public SuperVO[] queryHeadWithOtherCon(Class headClass,
			String[] tableNames, String[] joinCons, String strWhere)
			throws Exception {

		if (tableNames == null || tableNames.length == 0
				|| joinCons.length != (tableNames.length - 1)) {
			return null;
		}
		StringBuffer sql = new StringBuffer("select distinct ");
		sql.append(tableNames[0]).append(".*");
		sql.append(" from ").append(tableNames[0]);
		for (int i = 1; i < tableNames.length; i++) {
			sql.append(" inner join ");
			sql.append(tableNames[i]).append(" on ");
			sql.append(joinCons[i-1]);
		}
		sql.append(" where ");
		sql.append(tableNames[0]).append(".dr=0");
		for (int i = 1; i < tableNames.length; i++) {
			sql.append(" and ").append(tableNames[i]);
			sql.append(".dr=0");
		}
		if (!StringUtil.isEmptyWithTrim(strWhere)) {
			sql.append(" and (").append(strWhere).append(")");
		}
		SuperVO[] rs = null;
		List list = (ArrayList) new BaseDAO().executeQuery(sql.toString(),
				new BeanListProcessor(headClass));
		if (list != null) {
			rs = (SuperVO[]) list.toArray((SuperVO[]) Array.newInstance(
					headClass, 0));
		}
		return rs;
	}

}
